// set function parseTime,formatTime to filter
export { parseTime, formatTime } from '@/utils'

function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/* 数字 格式化*/
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}


//下载文件
export function downFile(names, res) {
  let blob = new Blob([res.data], {
    type:
      "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  });
  if (window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(blob);
  } else {
    let elink = document.createElement("a");
    let d = new Date().format("yyyyMMddhhmmss");
    elink.download = names + d + ".xls";
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    document.body.removeChild(elink);
  }
}
/**手机号 */
export function phonereg(value, M) {
  if (value) {
    let num = value + ""
    return num.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
  }
}
/**table 所有处理方式   */
export function commtable(value, M) {
  // console.log(value, M)
  // if(val){
  //   for(let i in val){
  //     console.log(i)

  //   }
  // }

  var numreg = /^\d*$/;
  var reDateTime = /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;

  if (value) {

    if (M.indexOf('电话') > -1 || M.indexOf('手机号') > -1) {
      // 手机号处理
      let num = value + ""
      return num.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
    } else if (numreg.test(value) && M.indexOf('时间') > -1) {
      //时间戳的处理
      if ((value + '').length == 10) {
        value = value * 1000
      }
      var date = new Date(value);
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? ('0' + m) : m;
      var d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      var h = date.getHours();
      h = h < 10 ? ('0' + h) : h;
      var minute = date.getMinutes();
      var second = date.getSeconds();
      minute = minute < 10 ? ('0' + minute) : minute;
      second = second < 10 ? ('0' + second) : second;
      let timeval = `${y}-${m}-${d} ${h}:${minute}:${second}`
      if (reDateTime.test(timeval))
        return timeval
    } else {
      return value
    }
  }
}